var pattern = new RegExp(/\?OrderNo=(\S*)/g);
var idx = pattern.exec(window.location.href);

var _grid = $('#jgTable'),
    grid0s = $('#jgTable0s'),
    OrderNo = $('input[data-field="OrderNo"]'),
    CardCode = $('input[data-field="CardCode"]'),
    CardName = $('input[data-field="CardName"]'),
    UserCode = $('input[data-field="UserCode"]'),
    UserName = $('input[data-field="UserName"]'),
    DocStatus = $('#DocStatus'),
    CancelReason = $('#CancelReason'),
    CreateDate = $('#CreateDate'),
    UpdateDate = $('#UpdateDate'),
    DeliverDate = $('#DeliverDate'),
    DocTotal = $('input[data-field="ItemDocTotal"]'),
    iComment = $('input[data-field="Comment"]'),
    TaxCompany = $('#TaxCompany'),
    TaxType = $('#TaxType'),
    TaxBank = $('#TaxBank'),
    TaxPhone = $('#TaxPhone'),
    TaxAddress = $('#TaxAddress'),
    TaxAccount = $('#TaxAccount'),
    TaxNumber = $('#TaxNumber'),
    AddressId = $('#AddressId'),
    Province = $('#Province'),
    City = $('#City'),
    Area = $('#Area'),
    Street = $('#Street'),
    Address = $('#Address'),
    Receiver = $('#Receiver'),
    ReceiverPhone = $('#ReceiverPhone');

var _Data = [];
// for(var i=0;i<10;i++){
//     _Data.push({});
// }

// 日历
// $('.form_datetime').datetimepicker({
//     //minView: "month", //选择日期后，不会再跳转去选择时分秒 
//     language:  'zh-CN',
//     format: 'yyyy-mm-dd hh:ii:ss',
//     todayBtn:  1,
//     autoclose: 1,
// });

$(function(){
    requestSelect();
    requestAjax();
    _gridInit();
    $(".input-card i.fa-plus-circle").on('click', add0s);
    $("li[data-opt='back']").on('click', back); 
    $('.modal-data').draggable(); 
});

//MDM001402
function requestSelect(){
    var postData0s = {
        Method: 'Q',
        Content: {
            SearchText: ''
        }
    }
    for(i in postData){
        postData0s[i] = postData[i]
    }
    var gridData0s = Object.create(gridData);
    gridData0s.grid = $('#jgTable0s');
    gridData0s.pager = $('#jgPager0s');
    gridData0s.url = mdmPath + '/MDM0014/MDM001402';
    gridData0s.height = '390px';
    gridData0s.colModel = [
        {label:'客户代码', name:'Code'},
        {label:'客户名称', name:'Name'}
    ];
    gridData0s.sortname = 'Code';
    gridData0s.beforeRequest = function(){
        gridData0s.grid.jqGrid("setGridParam", {
            postData : postData0s,
        });
    };
    gridData0s.loadComplete = function(xhr){
        $("li[data-opt='opt_search']").find(">i").unbind('click').bind('click', function () {
            postData.Content.SearchText = $("#opt_search").find(">input").val();
            gridData0m.grid.trigger("reloadGrid");
        });
    }
    gridData0s.multiselect = true;
    gridInit(gridData0s);    
}

function requestAjax(){ //MDM0001/MDM000103
    if (idx) {
        var _postData = Object.create(postData);    //请求头
        _postData.Method = 'Q';
        _postData.Content = {
            OrderNo: idx[1]
        }
        $.ajax({
            async: false,
            type: 'post',
            url: omsPath + '/OMS0001/OMS000103',
            data: _postData,
            dataType: 'json',
            success: function (re) {
                if (re.Status == 200) {
                    loadAjax(re);
                } else if (re.Status == -999) {
                    alert_QX($('body'), re.Message);
                } else {
                    alert(re.Message);
                }
            }
        });
    }
}
function loadAjax(re){
    console.log(re.Content);
    OrderNo.val(re.Content.OrderNo);
    CardCode.val(re.Content.CardCode);
    CardName.val(re.Content.CardName);
    UserCode.val(re.Content.UserCode);
    UserName.val(re.Content.UserName);
    DocStatus.val(re.Content.DocStatus);
    DocTotal.val(re.Content.DocTotal);
    CancelReason.val(re.Content.CancelReason);
    CreateDate.val(re.Content.CreateDate);
    UpdateDate.val(re.Content.UpdateDate);
    DeliverDate.val(re.Content.DeliverDate);
    iComment.val(re.Content.Comment);
    TaxCompany.val(re.Content.TaxCompany);
    TaxType.val(re.Content.TaxType);
    TaxBank.val(re.Content.TaxBank);
    TaxPhone.val(re.Content.TaxPhone);
    TaxAddress.val(re.Content.TaxAddress);
    TaxAccount.val(re.Content.TaxAccount);
    TaxNumber.val(re.Content.TaxNumber);
    AddressId.val(re.Content.AddressId);
    Province.val(re.Content.Province);
    City.val(re.Content.City);
    Area.val(re.Content.Area);
    Street.val(re.Content.Street);
    Address.val(re.Content.Address);
    Receiver.val(re.Content.Receiver);
    ReceiverPhone.val(re.Content.ReceiverPhone);
    _Data = re.Content.OMS000102;
}

function alarmFormatter(cellvalue, options, rowdata){
    var grid = $(this);
    if(JSON.stringify(rowdata) !== '{}'){
        cellvalue = '<a href="javascript:0" opt-data="'+grid[0].id+'_delete" title="删除"><i class="fa fa-trash-o"></i></a>';
    }else{
        cellvalue = '';
    }
    return cellvalue;
}
function _gridInit(){
    var _gridData = Object.create(gridData);
    _gridData.grid = $('#jgTable');
    _gridData.datatype = "local";
    _gridData.rowNum = '100';
    _gridData.colModel = [
        {label:'行号', name:'LineID', hidden:true},
        {label:'排序行号', name:'OrderById', hidden:true},
        {label:'订单编号', name:'OrderNo', hidden:true},
        {label:'SKU', name:'SKU'},
        {label:'物料描述', name:'ItemName'},
        {label:'基于类型', name:'BaseType'},
        {label:'核心规格', name:'Mainproperty'},
        {label:'价格', name:'Price'},
        {label:'数量', name:'Quantity'},
        {label:'折扣', name:'Discount'},
        {label:'行总计', name:'LineTotal'},
        {label:'行备注', name:'LineComment'},
        {label:'来源', name:'Source', hidden:true},
        {label:'创建时间', name:'CreateDate', hidden:true},
        {label:'更新时间', name:'UpdateDate', hidden:true},
        {label:'操作', name:'Options', hidden:true, width:60, formatter:alarmFormatter, sortable:false}
    ];
    _gridData.scroll = true;
    _gridData.height = '390px';
    _gridData.beforeRequest = function(){
        _gridData.grid.jqGrid("setGridParam", {
            data : _Data,
        });
    };
    _gridData.loadComplete = function(xhr){
        gridSize();
        // var rowData = gridData02.grid.jqGrid("getRowData");
        // $('a[opt-data="jgTable02_delete"]').unbind('click').bind('click',function(){
        //     var rowId = $(this).parent().parent()[0].id;
        //     gridData02.grid.jqGrid("delRowData",rowId);
        //     Data02.forEach(function(v,i,a){
        //         v.SKU===rowData[rowId-1].SKU&&Data02.splice(i,1);
        //     });
        //     console.log(Data02)
        // });
        // $('a[opt-data="jg_add"]').unbind('click').bind('click', addSKU);
    };
    gridInit(_gridData);
}

//Options
function back(){
    window.open('OMS000106.html', '_self');
}
function add0s(){
    $('#Modal0s').removeClass('hidden');
    gridSize();
    $('#addBtn2').unbind('click').bind('click', function(){
        var rowIds = grid0s.jqGrid('getGridParam', 'selarrrow');
        // console.log(rowIds)
        var addRowData = grid0s.jqGrid("getRowData",rowIds[0]);	//这是获得 某一行的数据
        // console.log(addRowData)
        CardCode.val(addRowData.Code);
        CardName.val(addRowData.Name);
        $('#Modal0s').addClass('hidden');
        grid0s.jqGrid("clearGridData").trigger("reloadGrid");
    });
}

//样式
function gridSize(){
    _grid.setGridWidth($('.u_tabs_container').width() - 30);
    grid0s.setGridWidth($('.u_modal_body').eq(0).width());
}


